package com.luo.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.luo.ssm.entity.SysFile;
import com.luo.ssm.entity.SysFolder;
import com.luo.ssm.entity.SysUser;
import com.luo.ssm.service.SysFileService;
import com.luo.ssm.service.SysFolderService;
import com.luo.ssm.vo.FileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author : luocheng
 * @date : Created in 2021/8/28 18:14
 * @description :
 */
@Controller
@RequestMapping("/fileManage")
public class SysFileController {
    @Autowired
    private SysFileService sysFileService;

    /**
     * 分页查询文件列表
     * @param fileVo
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping(value = "/list/{uid}",params = "uid!=-1")
    public String getList(FileVo fileVo, @RequestParam(value = "currentPage",required = false,defaultValue = "1") int pageNum, @RequestParam(required = false,defaultValue = "5") int pageSize, Model model){
        PageInfo<FileVo> pageInfo = sysFileService.getAllFile(fileVo, pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        //存储查询关键字用于回显
        model.addAttribute("keyword",fileVo.getKeyword());
        return "file/list";
    }

    /**
     * 上传文件前的查询，查询所有文件夹名和id
     * @param model
     * @return
     */
    @RequestMapping("/folderList")
    public String beforeAdd(Model model,@RequestParam("uid") Integer uid){
        List<SysFolder> allFolder = sysFileService.getAllFolder(uid);
        model.addAttribute("folderList",allFolder);
        return "file/add";
    }

    /**
     * 上传文件，可多文件上传
     * 由于上传文件设置了表单的enctype="multipart/form-data",不能用request获取到值，
     * 因此用注解@RequestParame来获取值
     * @param multipartFiles
     * @param session
     * @param folderValue
     * @return
     */
    @RequestMapping("/upload")
    public String upload(@RequestParam("uploadFile") MultipartFile[] multipartFiles,HttpSession session,@RequestParam("folderValue") String folderValue){
        //从session中获取登录名
        SysUser user = (SysUser) session.getAttribute("user");
        String uloginname = user.getUloginname();
        //文件存储路径
        String filePath;
        //获取要存储文件夹的id和文件名,获取到值得格式  1-xxx
        int index = folderValue.indexOf("-");
        String tid = folderValue.substring(0, index);
        String tname = folderValue.substring(index + 1);
        //完整文件夹名--upload/登录名/文件夹名
        String fullFoderName="upload/"+uloginname+"/"+tname;
        //获取文件要存储到的位置，若该位置不存在，则创建
        String realPath = session.getServletContext().getRealPath("/"+fullFoderName);
        File fileDir = new File(realPath);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        //获取要上传的原始文件名
        for (MultipartFile file : multipartFiles) {
            //获取要上传的原始文件名(包含扩展名)
            String uploadName = file.getOriginalFilename();
            //大小-字节
            long size = file.getSize();
            //类型
            String contentType = file.getContentType();
            if (!"".equals(uploadName)) {
                //目标文件
                File targetFile = new File(realPath, uploadName);
                //将上传文件写入到目标文件,并添路径至集合
                try {
                    file.transferTo(targetFile);
                    filePath=session.getServletContext().getContextPath() + "/"+fullFoderName+"/" + uploadName;
                    //向数据库中存储信息
                    SysFile sysFile = new SysFile();
                    sysFile.setFilename(uploadName);
                    sysFile.setFcreatedate(new Date());
                    sysFile.setFcontenttype(contentType);
                    //字节转化为kb
                    sysFile.setFsize(Double.valueOf(size)/1024);
                    sysFile.setFpath(filePath);
                    sysFile.setTid(Integer.valueOf(tid));
                    System.out.println(sysFile);
                    sysFileService.addNewFileInfo(sysFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //重定向到列表页面
        return "redirect:/fileManage/list/"+user.getUid();
    }
}
